//
//  VPaiDeviceSetting.swift
//  VPai
//
//  Created by devel on 16/9/29.
//  Copyright © 2016年 viatech. All rights reserved.
//

import Foundation

class VPaiDeviceSetting {
    
    private static let KEY_VIDEO_RESOLUTION_WIDTH = "key_video_resolution_width"
    private static let KEY_VIDEO_RESOLUTION_HEIGHT = "key_video_resolution_height"
    private static let KEY_PREVIEW_SOUND = "key_preview_sound"
    
    //单例模式
    class var shareInstance : VPaiDeviceSetting{
        struct Static {
            static var instance: VPaiDeviceSetting?
            static var token: dispatch_once_t = 0
        }
        
        dispatch_once(&Static.token){
            Static.instance = VPaiDeviceSetting()
        }
        
        return Static.instance!
    }
    
    var videoResolutionWidth : Int = 1920{
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setInteger(videoResolutionWidth, forKey: VPaiDeviceSetting.KEY_VIDEO_RESOLUTION_WIDTH)
        }
    }
    
    var videoResolutionHeight : Int = 960{
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setInteger(videoResolutionHeight, forKey: VPaiDeviceSetting.KEY_VIDEO_RESOLUTION_HEIGHT)
        }
    }
    
    var previewSound : Bool = false{
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setBool(previewSound, forKey: VPaiDeviceSetting.KEY_PREVIEW_SOUND)
        }
    }
    
    private init(){
        let defs = NSUserDefaults.standardUserDefaults()
        
        videoResolutionWidth = defs.integerForKey(VPaiDeviceSetting.KEY_VIDEO_RESOLUTION_WIDTH)
        videoResolutionHeight = defs.integerForKey(VPaiDeviceSetting.KEY_VIDEO_RESOLUTION_HEIGHT)
        if videoResolutionWidth == 0 && videoResolutionHeight == 0 {
            videoResolutionWidth = 1920
            videoResolutionHeight = 960
        }
        
        previewSound = defs.boolForKey(VPaiDeviceSetting.KEY_PREVIEW_SOUND)
    }
}
